package neu.soft.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import neu.soft.entity.DO.ReceiptJournal;
import neu.soft.exception.AppException;
import neu.soft.exception.AppExceptionCodeMsg;
import neu.soft.mapper.ReceiptJournalMapper;
import neu.soft.query.BasePageQuery;
import neu.soft.service.ReceiptJournalService;
import neu.soft.util.SnowflakeIdWorker;
import neu.soft.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ReceiptJournalServiceImpl implements ReceiptJournalService {

    @Autowired
    ReceiptJournalMapper receiptJournalMapper;

    @Override
    public void addJournal(ReceiptJournal receiptJournal) {
        SnowflakeIdWorker snowflakeIdWorker = SnowflakeIdWorker.getInstance();
        Long journalId = snowflakeIdWorker.nextId();
        receiptJournal.setJournalId(journalId);
        int res = receiptJournalMapper.addReceiptJournal(receiptJournal);
        if (res < 1) {
            throw new AppException(AppExceptionCodeMsg.DZYJ_NGDZ);
        }
    }

    @Override
    public PageBean<ReceiptJournal> findByPage(BasePageQuery basePageQuery) {
        PageBean<ReceiptJournal> pageBean = new PageBean<>();
        PageHelper.startPage(basePageQuery.getPageNow(), basePageQuery.getPageSize());
        List<ReceiptJournal> receiptJournalList = receiptJournalMapper.findByPage(basePageQuery);
        PageInfo<ReceiptJournal> pageInfo = new PageInfo<>(receiptJournalList);
        pageBean.setTotalRows(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setRowData(pageInfo.getList());
        return pageBean;

    }

    @Override
    public ReceiptJournal findById(String oid) {
        ReceiptJournal receiptJournal = receiptJournalMapper.findByOid(oid);
        if (receiptJournal == null) {
            throw new AppException(AppExceptionCodeMsg.QUERY_FAIL);
        }
        return receiptJournal;
    }
}
